package com.nervenets.web.controller.handler.user;

import com.nervenets.web.controller.handler.Handler;
import com.nervenets.web.controller.handler.common.TokenParams;
import com.nervenets.web.entity.MessageCode;
import com.nervenets.web.enumeration.OperateType;
import com.nervenets.web.enumeration.OrderStatus;
import com.nervenets.web.enumeration.OrderType;
import com.nervenets.web.enumeration.WealthType;
import com.nervenets.web.hibernate.domain.user.Order;
import com.nervenets.web.hibernate.domain.user.User;
import com.nervenets.web.redis.domain.movie.MovieStored;
import com.nervenets.web.redis.domain.user.WealthOfMoneyByOperateType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class UserCenterHandler extends Handler<TokenParams> {
    @Override
    public String handle(TokenParams params, HttpServletRequest request) throws Exception {
        User user = userDao.findById(params.getUserId(), User.class);
        if (null == user) return errorMessage(MessageCode.code_401);
        long surplus = userDao.getSurplus(params.getUserId(), WealthType.money);
        long score = userDao.getSurplus(params.getUserId(), WealthType.score);
        long reward = userDao.getSurplus(params.getUserId(), WealthType.reward);

        UserModel userModel = new UserModel(user, 1, globalVariablesService.getGlobalRule().getDefaultAvatar());
        userModel.setSurplus(surplus);
        userModel.setScore(score);
        userModel.setReward(reward);

        LastOrderModel lastOrder = null;
        List<Object> objects = new ArrayList<>();
        objects.add(params.getUserId());
        objects.add(OrderType.movie);
        objects.add(OrderStatus.init);
        final List<Order> orders = userDao.getAllObjectsByHql("from Order where userId = ? and orderType = ? and status > ? order by id desc ", objects, 0, 1);
        if (!orders.isEmpty()) {
            final Order order = orders.get(0);
            lastOrder = new LastOrderModel(order, globalService.getMovie(order.getUnionId()));
        }

        objects = new ArrayList<>();
        objects.add(params.getUserId());
        /*objects.add(params.getUserId());
        BigDecimal countOfByHql = userDao.getCountOfByHql("from User where inviteUserId = ? or teamUserId = ? ", objects);*/
        BigDecimal countOfByHql = userDao.getCountOfByHql("from User where inviteUserId = ? ", objects);//暂时只显示一级的数据
        WealthOfMoneyByOperateType dividends = globalService.getUserWealthOfMoneyByOperateType(params.getUserId(), OperateType.dividends, WealthType.reward);

        InviteModel inviteModel = new InviteModel(dividends.getTotal(), countOfByHql.intValue());
        String customServiceTel = globalVariablesService.getGlobalRule().getCustomServiceTel();

        return successMessage(userModel
                , new KeyValue("lastOrder", lastOrder)
                , new KeyValue("invite", inviteModel)
                , new KeyValue("customServiceTel", customServiceTel)
        );
    }

    @Override
    public String result() throws Exception {
        return successMessage(new UserModel()
                , new KeyValue("lastOrder", new LastOrderModel())
                , new KeyValue("invite", new InviteModel())
                , new KeyValue("customServiceTel", "13800138000")
        );
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    class LastOrderModel {
        private int createTime;
        private long orderId;
        private int number;
        private long priceOfUnit;
        private boolean offlinePay;
        private long deposit;
        private OrderStatus orderStatus;

        private String movieTitle = "影片名称";
        private String movieCoverImage = "影片封面图片";

        public LastOrderModel(Order order, MovieStored movie) {
            this.createTime = order.getCreateTime();
            this.orderId = order.getId();
            this.number = order.getExtra().getNumber();
            this.priceOfUnit = order.getExtra().getPriceOfUnit();
            this.offlinePay = order.isOfflinePay();
            this.deposit = order.getExtra().getDeposit();
            this.orderStatus = order.getStatus();

            this.movieTitle = movie.getTitle();
            this.movieCoverImage = movie.getCoverImage();
        }
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    class InviteModel {
        private long income;
        private int sum;
    }
}
